﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MaxLeaf.Common;
using System.Data.SqlClient;
using MaxLeaf.DB;
using System.Data;


namespace MaxLeaf.DAL.K2K
{
    public class t_ComCategoryDAL : BaseDAL
    {
        /// <summary>
        /// 类别
        /// </summary>
        public t_ComCategoryDAL()
            : base()
        {
            strTableName = "t_ComCategory";
            strUpdateFNumberTableName = "t_ComCategory";
            strFieldNames = "FNumber,FName,FTaxRate,FOrderFlag,FIsAllowLong,FCanModifyPrice,FPointsRate";

            strFItemClassID = Const.FItemClassID_Category;
        }

        

        public override int Update(DBAccess dbOrgK3Con, DBAccess dbDestK3Con, string strKeyValue)
        {
            KeyValuePair<string, IDataParameter[]> kvp = dbOrgK3Con.CreateCondParam(new Dictionary<string, object>() { { "FitemID", strKeyValue } });
            DataRow drOrg = dbOrgK3Con.GetDataFirstRow(string.Format("select fdetail from t_item where {0}", kvp.Key), kvp.Value);
            if (drOrg == null) throw new MaxLeafException(string.Format("t_item not exist FitemID {0}", strKeyValue));

            if (!CvtFunc.ToBoolean(drOrg["fdetail"]))
            {
                return new t_ComCategoryGroupDAL().Update(dbOrgK3Con, dbDestK3Con, strKeyValue);
            }
            return base.Update(dbOrgK3Con, dbDestK3Con, strKeyValue);
        }

        

        protected override void DAL_GetFieldData(DBAccess dbOrgK3Con, DBAccess dbDestK3Con, string strFieldName, DataRow drOrgData, DataRow drDestData)
        {
            if (strFieldName.Equals("FitemID", StringComparison.InvariantCultureIgnoreCase))
            {
                if (CvtFunc.ToInt(drDestData[strFieldName]) != 0) return;
                string[] arrFnumber = CvtFunc.ToString(drOrgData["Fnumber"]).Split('.');
                string strFParentID = string.Empty;
                string strOrgFParentID = dbOrgK3Con.GetValue("select FitemID from t_item where FNumber=@FNumber and FItemClassID=" + strFItemClassID, new SqlParameter[] { new SqlParameter("@FNumber", string.Join(".", arrFnumber, 0, arrFnumber.Length - 1)) });
                if (arrFnumber.Length > 1)
                {
                    strFParentID = CvtFunc.ToString(new t_ComCategoryDAL().GetDestMapKeyValue(dbOrgK3Con, dbDestK3Con, strOrgFParentID));


                }
                else
                {
                    strFParentID = "0";
                };

                //再FitemID
                drDestData[strFieldName] = GetFItemID(dbOrgK3Con, dbDestK3Con, strOrgFParentID, strFParentID, arrFnumber);


            }
            else if (strFieldName.Equals("FOrderFlag", StringComparison.InvariantCultureIgnoreCase))
            {
                drDestData[strFieldName] = GetFTypeID(dbOrgK3Con, dbDestK3Con, CvtFunc.ToString(drOrgData[strFieldName]));
            }
            else
            {
                base.DAL_GetFieldData(dbOrgK3Con, dbDestK3Con, strFieldName, drOrgData, drDestData);
            }
        }

        private string GetFTypeID(DBAccess dbOrgK3Con, DBAccess dbDestK3Con, string strOrgFInterID)
        {
            t_SubMessageDAL dal = new t_SubMessageDAL();
            return dal.GetFInterID(dbOrgK3Con, dbDestK3Con, strOrgFInterID);
        }

        private string GetFItemID(DBAccess dbOrgK3Con, DBAccess dbDestK3Con, string strOrgFParentID, string strFParentID, string[] arrFnumber)
        {
            
            const int intFDetail = 1;
            string strFItemId = string.Empty;
            t_ItemDAL dal = new t_ItemDAL(strFItemClassID, strUpdateFNumberTableName);


            strFItemId = dal.GetFItemId(dbOrgK3Con, dbDestK3Con, arrFnumber.Length, strOrgFParentID, strFParentID, string.Join(".", arrFnumber, 0, arrFnumber.Length), intFDetail);

            UpdateAccess(dbDestK3Con, strFItemId, strFParentID);


            return strFItemId;
        }

       

        private void UpdateAccess(DBAccess dbDestK3Con, string strFItemId, string strFParentID)
        {
            string strSql = @"
                if not exists(select * from access_t_ComCategory where FitemID=@FitemID)
                begin
                 Insert into access_t_ComCategory(FitemID,FParentIDX,FDataAccessView,FDataAccessEdit,FDataAccessDelete)
                 Values(@FitemID,@FParentIDX,convert(varbinary(7200),REPLICATE(char(255),100)),convert(varbinary(7200),REPLICATE(char(255),100)),convert(varbinary(7200),REPLICATE(char(255),100)))
                end
                ";
            dbDestK3Con.ExecuteSql(strSql, new SqlParameter[] { new SqlParameter("@FitemID", strFItemId), new SqlParameter("@FParentIDX", strFParentID) });
        }

         

        protected override DataTable DAL_GetData(DBAccess dbOrgK3Con, DBAccess dbDestK3Con, int intdataType, object objParam)
        {
            GetDataType dataType = (GetDataType)intdataType;

            if (dataType == GetDataType.OrgData)
            {
                return Gett_ComCategory(dbOrgK3Con, CvtFunc.ToString(objParam));
            }


            return base.DAL_GetData(dbOrgK3Con, dbDestK3Con, intdataType, objParam);
        }



        private DataTable Gett_ComCategory(DBAccess dbOrgK3Con, string strFItemID)
        {
            DataTable dtt_ComCategory = null;
            if (dtt_ComCategory == null)
            {
                dtt_ComCategory = dbOrgK3Con.GetTableData("t_ComCategory", new Dictionary<string, object> { { "FitemID", strFItemID } });

            }
            return dtt_ComCategory;
        }

    }
}